package com.example.reception.controller;

import com.example.reception.controller.dto.UserDTO;
import com.example.reception.entity.Address;
import com.example.reception.entity.User;
import com.example.system.model.ApiResponse;
import com.example.reception.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author: adnaan
 * @create: 2023-10-22 22:07
 * @Description:
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public ApiResponse register(@RequestBody User user) {
        return userService.register(user);
    }

    @GetMapping("/isLoggedIn")
    public ApiResponse isLoggedIn(@RequestParam String username) {
        return userService.isLoggedIn(username);
    }

    @PostMapping("/login")
    public ApiResponse login(@RequestBody UserDTO userDTO) {
        String username = userDTO.getUsername();
        String password = userDTO.getPassword();
        return userService.login(username, password);
    }

    @PostMapping("/loginOut")
    public ApiResponse loginOut(@RequestBody UserDTO userDTO) {
        return userService.loginOut(userDTO.getUsername());
    }

    @PostMapping("/saveUser")
    public ApiResponse saveUser(@RequestBody User user) {
        return userService.saveUser(user);
    }

    @DeleteMapping("/delUser")
    public ApiResponse delUser(@RequestParam String userId){
        return userService.delUser(userId);
    }

    @GetMapping("/getAddress")
    public ApiResponse getAddress(@RequestParam String username){
        return userService.getAddress(username);
    }

    @PostMapping("/updateAddress")
    public ApiResponse updateAddress(@RequestBody Address address){
        return userService.updateAddress(address);
    }

    @DeleteMapping("/delAddress")
    public ApiResponse delAddress(@RequestParam String delId){
        return userService.delAddress(delId);
    }

    @PostMapping("/uploadAvatar")
    public ApiResponse uploadAvatar(@RequestParam MultipartFile file,@RequestParam String username) throws IOException {
        return userService.uploadAvatar(file,username);
    }

    @GetMapping("/page")
    public ApiResponse findPage(@RequestParam Integer currentPage,
                                @RequestParam Integer pageSize,
                                @RequestParam String userName
    ) {
        return userService.findPaginated(currentPage, pageSize, userName);
    }
}
